package br.ufrj.neg.reuniao.servlet;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;

import br.ufrj.neg.bean.Idioma;
import br.ufrj.neg.constants.General;
import br.ufrj.neg.preparacao.bean.NegociacaoBean;
import br.ufrj.neg.preparacao.model.NegociacaoModel;
import br.ufrj.neg.reuniao.bean.ReuniaoBean;
import br.ufrj.neg.reuniao.bean.TipoReuniaoBean;
import br.ufrj.neg.reuniao.model.ReuniaoModel;
import br.ufrj.neg.servlet.PageContext;
import br.ufrj.neg.usuario.bean.UsuarioBean;

public class ListarReuniaoAction extends VelocityViewServlet {

	private static final long serialVersionUID = -3113158265371305805L;

	// private static Logger log = Logger.getLogger("rootLogger");

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		ReuniaoModel rm = new ReuniaoModel();
		Idioma idioma = (Idioma) request.getAttribute("idioma");
		try {
			/*
			 * Pega a sessao
			 */
			HttpSession session = request.getSession(false);

			String idNegocio = null;
			if (request.getParameter("idNegocio") != null)
				idNegocio = request.getParameter("idNegocio");
			else if (session.getAttribute("idNegocio") != null) {
				idNegocio = (String) session.getAttribute("idNegocio");
				synchronized (session) {
					session.removeAttribute("NegociacaoBean");

				}
			}

			NegociacaoModel nm = new NegociacaoModel();
			NegociacaoBean nb = new NegociacaoBean();
			UsuarioBean ub;

			synchronized (session) {
				nb = nm.getNegociacao(Integer.parseInt(idNegocio));
				ub = (UsuarioBean) session.getAttribute("usuarioBean");
				session.removeAttribute("ReuniaoBean");
			}
			Collection<ReuniaoBean> rbLst=null;			
			if (nb != null) {
				TipoReuniaoBean trb = new TipoReuniaoBean(idioma);
				rbLst = rm.listaReuniaoporNegociacao(nb.getIdNegociacao(), trb);			
			}
			
			setContentType(request, response);
			Context context = createContext(request, response);
			fillContext(context, request);
			
			if (rbLst != null) {				
				//Formatando as datas de cada reuniao do rbLst
				for (ReuniaoBean rb : rbLst) {				
					rb.setDataFormatada(idioma.getDataReuniao(rb.getData()));				
				}
				
				request.setAttribute("reunioes",rbLst);
			}
			
			context.put("reuniaoDataSet", rbLst);			
			context.put("pageContext", new PageContext(request));			
			context.put("idNegocio", idNegocio);

			Template template = getTemplate(General.TEMPLATE_REUNIAO_PATH + "listaReuniao.vm");
			mergeTemplate(template, context, response);

		} catch (Exception e) {
			System.out.println(e);
			request.getRequestDispatcher(General.TELA_ERRO_500).forward(request,
					response);
		}
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doPost(request, response);
	}

}
